package com.jdk.demo.asynchronousFileChannel.写入数据;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Test1 {

	public static void main(String[] args) throws Exception {
		Path path = Paths.get("s.txt");
		AsynchronousFileChannel channel =  AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
		ByteBuffer buffer = ByteBuffer.wrap("abdce".getBytes());
		// channel.size(): 相当于追加内容
		channel.write(buffer, channel.size(), "我是附加的参数", new CompletionHandler<Integer, String>() {
			@Override
			public void completed(Integer result, String attachment) {
				System.out.println("public void completed(Integer result, String attachment) result=" + result + " attachment=" + attachment);
			}

			@Override
			public void failed(Throwable exc, String attachment) {
				System.out.println("public void failed(Throwable exc, String attachment) attachment" + attachment);
				System.out.println("getMessage=" + exc.getMessage());
			}
		});
		channel.close();
		Thread.sleep(2000);
	}

}
